package com.david.videoapp.activity

import android.annotation.SuppressLint
import android.view.View
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.DefaultRenderersFactory
import androidx.media3.exoplayer.ExoPlayer
//import androidx.media3.exoplayer.rtsp.RtspMediaSource
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory
import androidx.media3.ui.PlayerView
import com.david.videoapp.ChannelBean
import com.david.videoapp.exoplayer.BlackListMediaCodecSelector
import java.util.*

/**
 * TODO 播放rtsp及rtmp视频
 */
class ExoplayerVideoActivity:BaseVideoActivity() {
    private lateinit var localPlayerView: PlayerView
    private var exoPlayer: ExoPlayer? = null
    @SuppressLint("UnsafeOptInUsageError")
    override fun getVideoView(): View {
        localPlayerView = PlayerView(this)
        localPlayerView.requestFocus()

        exoPlayer = ExoPlayer.Builder(
            this,
            DefaultRenderersFactory(this).setMediaCodecSelector(BlackListMediaCodecSelector()),
            DefaultMediaSourceFactory(this)//RtspMediaSource.Factory()
        ).build()

        localPlayerView.player = exoPlayer

        localPlayerView.hideController()
        return localPlayerView
    }

    override fun getChannels(): ArrayList<ChannelBean> {
//        return JSON.parseArray(LiveDataHelper.instance.rtmpUrls, ChannelBean::class.java) as ArrayList<ChannelBean>
        return ArrayList()
    }

    override fun setVideoPath(videoPath: String) {
        exoPlayer!!.setMediaItem(MediaItem.Builder().setUri(videoPath).build())
    }

    override fun playVideo() {
        exoPlayer!!.playWhenReady = true
        exoPlayer!!.prepare()
    }

    override fun pauseVideo() {
        exoPlayer?.pause()
    }

    override fun stopPlay() {
        exoPlayer?.stop()
    }

    override fun isPlaying(): Boolean {
        return exoPlayer?.isPlaying!!
    }

    override fun onDestroy() {
        super.onDestroy()
        exoPlayer!!.release()
    }
}